export function configureFakeBackend() {
  console.log("配置");
  let users = [
    {
      id: 1,
      username: "20152030221",
      password: "123456",
      firstName: "Test",
      lastName: "User"
    }
  ];
  let realFetch = window.fetch;
  window.fetch = function(url, opts) {
    return new Promise((resolve, reject) => {
      // wrap in timeout to simulate server api call
      setTimeout(() => {
        // authenticate
        if (url.endsWith("/auth/login") && opts.method === "POST") {
          // get parameters from post request
          let params = JSON.parse(opts.body);
          console.log("后台拦截");
          // find if any user matches login credentials
          let filteredUsers = users.filter(user => {
            return (
              user.username === params.username &&
              user.password === params.password
            );
          });

          if (filteredUsers.length) {
            // if login details are valid return user details and fake jwt token
            let responseJson = {
              token: "fake-jwt-token"
            };
            resolve({
              ok: true,
              text: () => Promise.resolve(JSON.stringify(responseJson))
            });
          } else {
            // else return error
            reject("Username or password is incorrect");
          }

          return;
        }
        // get users
        if (url.endsWith("/auth/user") && opts.method === "POST") {
          // check for fake auth token in header and return users if valid, this security is implemented server side in a real application
          let params = JSON.parse(opts.body);
          console.log(params);
          if (
            opts.headers &&
            opts.headers.Authorization === "Bearer fake-jwt-token"
          ) {
            resolve({
              ok: true,
              text: () =>
                Promise.resolve(
                  JSON.stringify({
                    username: "Zyd",
                    user: {
                      follower: 0,
                      following: 0,
                      status: "ONLINE",
                      wachers: 0,
                      privacy: {
                        name: "Zyd"
                      }
                    }
                  })
                )
            });
          } else {
            // return 401 not authorised if token is null or invalid
            reject("Unauthorised");
          }

          return;
        }

        // get users
        if (url.endsWith("/users") && opts.method === "GET") {
          // check for fake auth token in header and return users if valid, this security is implemented server side in a real application
          if (
            opts.headers &&
            opts.headers.Authorization === "Bearer fake-jwt-token"
          ) {
            resolve({
              ok: true,
              text: () => Promise.resolve(JSON.stringify(users))
            });
          } else {
            // return 401 not authorised if token is null or invalid
            reject("Unauthorised");
          }

          return;
        }

        // pass through any requests not handled above
        realFetch(url, opts).then(response => resolve(response));
      }, 500);
    });
  };
}
